package comparisons

import (
	"jvm/instructions/base"
	"jvm/rtda"
)

// Compare double
type DCMPG struct{ base.NoOperandsInstruction }

func (dc *DCMPG) Execute(jFrame *rtda.JFrame) {
	_dcmp(jFrame, true)
}

type DCMPL struct{ base.NoOperandsInstruction }

func (dc *DCMPL) Execute(jFrame *rtda.JFrame) {
	_dcmp(jFrame, false)
}

func _dcmp(jFrame *rtda.JFrame, gFlag bool) {
	stack := jFrame.OperandStack()
	v2 := stack.PopDouble()
	v1 := stack.PopDouble()
	if v1 > v2 {
		stack.PushInt(1)
	} else if v1 == v2 {
		stack.PushInt(0)
	} else if v1 < v2 {
		stack.PushInt(-1)
	} else if gFlag {
		stack.PushInt(1)
	} else {
		stack.PushInt(-1)
	}
}
